package ge.telasi.tasks.ui.search;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.ui.task.TaskDecontationUtils;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

/**
 * Date search.
 *
 * @author dimitri
 */
public class TaskStatusSearch extends JPanel implements FieldEditor<TaskStatusField> {
    static final int[] STATUSES = { Task.STATUS_DRAFT, Task.STATUS_OPEN,
        Task.STATUS_PAUSED,
        Task.STATUS_COMPLETE_OK, Task.STATUS_COMPLETE_CANCEL,
        Task.STATUS_CLOSED_OK, Task.STATUS_CLOSED_CANCEL };

    private TaskStatusField proto;
    private List<JToggleButton> buttons = new ArrayList<JToggleButton>();

    public TaskStatusSearch() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
        for (int status : STATUSES) {
            JToggleButton button = new JToggleButton();
            button.setIcon(TaskDecontationUtils.getStatusIcon(status));
            button.setToolTipText(TaskDecontationUtils.getStatusName(status));
            button.setName(String.valueOf(status));
            add(button);
            buttons.add(button);
        }
    }

    public TaskStatusField getField() {
        TaskStatusField field = new TaskStatusField();
        List<Integer> statuses = new ArrayList<Integer>();
        for (JToggleButton button : buttons) {
            if (button.isSelected()) {
                int status = Integer.parseInt(button.getName());
                statuses.add(status);
            }
        }
        field.setStatuses(statuses);
        return field;
    }

    public void setField(TaskStatusField field) {
        this.proto = field;
    }

}
